package com.wisdudu.blank.support.widget.loading;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.LayoutRes;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.wisdudu.blank.R;
import com.wisdudu.blank.support.util.NetUtil;


//<com.wisdudu.estate.support.view.loading.LoadingFrameLayout
//        xmlns:android="http://schemas.android.com/apk/res/android"
//        xmlns:app="http://schemas.android.com/apk/res-auto"
//        android:id="@+id/fl_load"
//        android:layout_width="match_parent"
//        android:layout_height="match_parent"
//        android:layout_below="@id/title_bar"
//        app:emptyText="暂无商家申请"
//        app:errorText="加载失败，请重试"
//        app:errorIcon="@drawable/ic_null"
//        app:emptyIcon="@drawable/ic_null"
//        app:emptyRetryVisibility="visiable"
//        app:errorRetryVisibility="visiable">
//    <添加内容/>
//<com.wisdudu.estate.support.view.loading.LoadingFrameLayout/>

/**
 * Created by qyf on 2016/7/15.
 * 原有的LoadingManager初始化很不方便，深圳方式也并不优雅，这里使用自定义view来简化它。
 * 使用方法：
 * 如上注释，中间添加内容页
 * 调用，空数据时：showEmptyView() 发生错误：showErrorView()  显示内容页：showContentView()
 * <p>
 * 提供的自定义属性有（注意，均有默认值，可以不用写）：
 * emptyText erorrText：空页面，错误页面文字提示，默认为：暂无数据，加载错误
 * emptyIcon errorIcon ：空页面，错误页面图标 ，默认为：ic_null
 * emptyRetryVisibility errorRetryVisibility: 空页面，错误页面重试按钮是否显示，默认为：显示
 * 并提供 setOnLoadClickListener(new OnLoadClickListener())来回调重试点击事件
 * <p>
 * 如果以上不能满足，也提供自定义空，错误，loading布局，但是必须保持对应id不变
 */
public class LoadingFrameLayout extends FrameLayout {
    @LayoutRes
    private int mEmptyViewLayoutResId;
    @LayoutRes
    private int mErrorViewLayoutResId;
    @LayoutRes
    private int mLoadingViewLayoutResId;


    private int mContentViewId;

    private View emptyView;

    private View errorView;

    private View loadingView;

    private View contentView;
    private String mEmptyText;
    private String mEmptyRetryText;
    private Drawable mEmptyIcon;
    private String mErrorText;
    private Drawable mErrorIcon;
    private int mEmptyRetyrVisiable;
    private int mErrorRetyrVisiable;


    public LoadingFrameLayout(Context context) {
        this(context, null);
    }

    public LoadingFrameLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LoadingFrameLayout, defStyleAttr, 0);
        try {
//            final int N = a.getIndexCount();
//            for (int i = 0; i < N; i++) {
//                int attr=a.getIndex(i);
//                if(attr==R.styleable.LoadingFrameLayout_emptyView){
//                    setEmptyLayotuResId();
//                    a.getString()
//                }
//            }
            mEmptyViewLayoutResId = a.getResourceId(R.styleable.LoadingFrameLayout_emptyView, R.layout.view_load_empty);
            if (NetUtil.INSTANCE.isConnected()) {
                mErrorViewLayoutResId = a.getResourceId(R.styleable.LoadingFrameLayout_errorView, R.layout.view_load_error);
            } else {
                mErrorViewLayoutResId = a.getResourceId(R.styleable.LoadingFrameLayout_errorView, R.layout.view_network_error);
            }
            mLoadingViewLayoutResId = a.getResourceId(R.styleable.LoadingFrameLayout_loadingView, R.layout.view_load_fruit);
            mEmptyText = a.getString(R.styleable.LoadingFrameLayout_emptyText);
            if (mEmptyText == null) {
                mEmptyText = "暂无数据";
            }
            mEmptyRetryText = a.getString(R.styleable.LoadingFrameLayout_emptyRetryText);
            if (mEmptyRetryText == null) {
                mEmptyRetryText = "重新加载";
            }
            mEmptyIcon = a.getDrawable(R.styleable.LoadingFrameLayout_emptyIcon);
            if (mEmptyIcon == null) {
                mEmptyIcon = getResources().getDrawable(R.drawable.ic_null);
            }
            mEmptyRetyrVisiable = a.getInt(R.styleable.LoadingFrameLayout_emptyRetryVisibility, View.VISIBLE);

            mErrorText = a.getString(R.styleable.LoadingFrameLayout_errorText);
            if (mErrorText == null) {
                mErrorText = "加载错误";
            }
            mErrorIcon = a.getDrawable(R.styleable.LoadingFrameLayout_errorIcon);
            if (mErrorIcon == null) {
                if (NetUtil.INSTANCE.isConnected()) {
                    mErrorIcon = getResources().getDrawable(R.drawable.ic_null);
                } else {
                    mErrorIcon = getResources().getDrawable(R.drawable.nothing_icon_failed1);
                }
            }
            mErrorRetyrVisiable = a.getInt(R.styleable.LoadingFrameLayout_errorRetryVisibility, View.VISIBLE);


        } finally {
            a.recycle();
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        contentView = getChildAt(0);

        if (mEmptyViewLayoutResId != -1) {
            setEmptyView(mEmptyViewLayoutResId);
        }

        if (mErrorViewLayoutResId != -1) {
            setErrorView(mErrorViewLayoutResId);
        }

        if (mLoadingViewLayoutResId != -1) {
            setLoadingView(mLoadingViewLayoutResId);
        }
    }

    public void setEmptyView(View emptyView) {
        if (this.emptyView != emptyView) {
            if (this.emptyView != null) {
                removeView(this.emptyView);
            }
            this.emptyView = emptyView;

            addView(this.emptyView);
        }

    }

    public void setErrorView(View errorView) {
        if (this.errorView != null) {
            removeView(this.errorView);
        }
        if (this.errorView != errorView) {
            this.errorView = errorView;
            addView(errorView);
            this.errorView.setVisibility(GONE);
        }
    }

    public void setLoadingView(View loadingView) {
        if (this.loadingView != null) {
            removeView(this.loadingView);
        }
        if (this.loadingView != loadingView) {
            this.loadingView = loadingView;
            addView(loadingView);
            this.loadingView.setVisibility(GONE);
        }
    }

    public void setContentView(View contentView) {
        if (this.contentView != null) {
            removeView(this.contentView);
        }
        if (this.contentView != contentView) {
            this.contentView = contentView;
            addView(contentView);
        }

    }

    public void setEmptyView(@LayoutRes int emptyViewResId) {
        View view = LayoutInflater.from(getContext()).inflate(emptyViewResId, this, false);
        TextView tvEmpty = (TextView) view.findViewById(R.id.load_empty_text);
        tvEmpty.setText(mEmptyText);
        mEmptyIcon.setBounds(0, 0, mEmptyIcon.getMinimumWidth(), mEmptyIcon.getMinimumHeight());

        tvEmpty.setCompoundDrawables(null, mEmptyIcon, null, null);
        TextView emptyRetry = (TextView) view.findViewById(R.id.load_empty_retry);
        emptyRetry.setText(mEmptyRetryText);
        emptyRetry.setVisibility(mEmptyRetyrVisiable);
        emptyRetry.setOnClickListener(v -> {
            //空数据点击
            mOnLoadClickListener.onEmptyRetryClick(v);
        });
        setEmptyView(view);
    }

    public void setErrorView(@LayoutRes int errorViewResId) {
        View view = LayoutInflater.from(getContext()).inflate(errorViewResId, this, false);
        TextView tvError = (TextView) view.findViewById(R.id.load_error_text);
        tvError.setText(mErrorText);
        mErrorIcon.setBounds(0, 0, mErrorIcon.getMinimumWidth(), mErrorIcon.getMinimumHeight());

        tvError.setCompoundDrawables(null, mErrorIcon, null, null);
        TextView errorRetry = (TextView) view.findViewById(R.id.load_error_retry);
        errorRetry.setVisibility(mErrorRetyrVisiable);
        errorRetry.setOnClickListener(v -> {
            //错误页面点击
            mOnLoadClickListener.onErrorRetryClick(v);
        });
        setErrorView(view);
    }

    public void setErrorText(String errorText) {
        if (NetUtil.INSTANCE.isConnected()) {
            this.mErrorText = errorText;
        } else {
            this.mErrorText = "网络异常";
        }
    }

    public void setEmptyRetryText(String emptyRetryText) {
        this.mEmptyRetryText = emptyRetryText;
    }

    public void setLoadingView(@LayoutRes int loadingViewResId) {
        View view = LayoutInflater.from(getContext()).inflate(loadingViewResId, this, false);
        setLoadingView(view);
    }

    public void setContentView(@LayoutRes int contentViewResId) {
        View view = LayoutInflater.from(getContext()).inflate(contentViewResId, this, false);
        setContentView(view);
    }

    public void showEmptyView() {
        showSingleView(this.emptyView);
    }

    public void showErrorView() {
        showSingleView(this.errorView);
    }

    public void showLoadingView() {
        showSingleView(this.loadingView);
    }

    public void showContentView() {
        showSingleView(this.contentView);
    }

    private void showSingleView(View specialView) {
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (child == specialView) {
                child.setVisibility(VISIBLE);
            } else {
                child.setVisibility(GONE);
            }
        }
    }

    public void setmEmptyIcon(Drawable mEmptyIcon) {
        this.mEmptyIcon = mEmptyIcon;
    }

    OnLoadRetryClickListener mOnLoadClickListener;

    public interface OnLoadRetryClickListener {
        void onErrorRetryClick(View view);

        void onEmptyRetryClick(View view);
    }

    public void setOnLoadClickListener(OnLoadRetryClickListener onLoadClickListener) {
        mOnLoadClickListener = onLoadClickListener;
    }
}
